package turtle.system.invest;

public class BarSeries {
    private final static BarType TYPES[] = BarType.values();

    private final IndexSeries series[] = new IndexSeries[TYPES.length];
    private final int value[] = new int[TYPES.length];
    private int timestamp;

    public static BarSeries getBarSeries(Environment env, String name) {
        BarSeries bars = new BarSeries();
        for (BarType type : TYPES) {
            bars.series[type.ordinal()] = env.get(name + '.' + type.name().toLowerCase());
        }
        return bars;
    }

    public int get(BarType type) {
        return value[type.ordinal()];
    }

    public IndexSeries getSeries(BarType type) {
        return series[type.ordinal()];
    }

    public void setTimestamp(int timestamp) {
        this.timestamp = timestamp;
        for (BarType type : TYPES) {
            int index = type.ordinal();
            value[index] = series[index].getValue(timestamp);
        }
    }

    public boolean isBreakMaximum(BarType type, int days) {
    	if (timestamp - days < 0) return false;
        return get(type) > getSeries(type).getMaximum(timestamp - days, days);
    }

    public boolean isBreakMinimum(BarType type, int days) {
        if (timestamp - days < 0) return false;
    	return get(type) < getSeries(type).getMinimum(timestamp - days, days);
    }
}
